<?php

/**
 * 学生的增删改查
 */
//1. 跨域
header("Access-Control-Allow-Origin: *");
//2. 导入外部文件
include_once("./utils/db.php");
include_once("./utils/functions.php");
$ajaxReturn = ["code" => 0, "msg" => ""];
$students = [];
//3. 获取action，根据action的不同，调用不同的方法
//post方式定义action
if (isset($_POST["action"])) {
  if ($_POST["action"] == "save") {
    //获取值
    $student = [];
    $student["stuno"] = $_POST["stuno"];
    $student["stuname"] = $_POST["stuname"];
    $student["sex"] = $_POST["sex"];
    $student["phone"] = $_POST["phone"];
    $student["dorms"] = $_POST["dorms"];
    $student["classid"] = $_POST["classid"];
    $student["major"] = $_POST["major"];
    $student["departmentid"] = $_POST["departmentid"];
    $student["hobby"] = $_POST["hobby"];
    $student["birth"] = $_POST["birth"];
    $student["photo"] = $_POST["photo"];
    //返回保存信息
    echo addStudent($conn, $student);
  }
  if ($_POST["action"] == "update") {
    //获取值
    $student = [];
    $student["stuno"] = $_POST["stuno"];
    if (isset($_POST["stuname"])) {
      $student["stuname"] = $_POST["stuname"];
    }
    if (isset($_POST["sex"])) {
      $student["sex"] = $_POST["sex"];
    }
    if (isset($_POST["phone"])) {
      $student["phone"] = $_POST["phone"];
    }
    if (isset($_POST["dorms"])) {
      $student["dorms"] = $_POST["dorms"];
    }
    if (isset($_POST["classid"])) {
      $student["classid"] = $_POST["classid"];
    }
    if (isset($_POST["major"])) {
      $student["major"] = $_POST["major"];
    }
    if (isset($_POST["departmentid"])) {
      $student["departmentid"] = $_POST["departmentid"];
    }
    if (isset($_POST["hobby"])) {
      $student["hobby"] = $_POST["hobby"];
    }
    if (isset($_POST["birth"])) {
      $student["birth"] = $_POST["birth"];
    }
    if (isset($_POST["photo"])) {
      $student["photo"] = $_POST["photo"];
    }
    //返回保存信息
    echo updateStudent($conn, $student);
  }
}
//get方式定义action
if (isset($_GET["action"])) {
  if ($_GET["action"] == "getStudents") {
    echo getAllStudent($conn);
  }
  if ($_GET["action"] == "delete") {
    $stuno = $_GET["stuno"];
    echo deleteStudent($conn, $stuno);
  }
}
//4. 方法的编写
//获取所有学生
//getStudents
function getAllStudent($conn)
{
  $sql = "select * from xk_student";

  global $students;
  global $ajaxReturn;
  $results = mysqli_query($conn, $sql);
  //从结果集中获取记录数
  $num = mysqli_num_rows($results);
  if ($num > 0) {
    while ($row = mysqli_fetch_assoc($results)) {
      $students[] = $row;
    }
    $ajaxReturn = ["code" => 200, "msg" => "查询成功", "data" => $students];
  } else {
    $ajaxReturn = ["code" => 0, "msg" => "查询失败", "data" => null];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//学生查询
//添加学生
function addStudent($conn, $student)
{
  global $ajaxReturn;
  $sql = "insert into xk_student(stuno,stuname,sex,phone,dorms,classid";
  $sql .= ",major,departmentid,hobby,birth,photo) values(?,?,?,?,?,?,?,?,?,?,?)";
  $stmt = mysqli_prepare($conn, $sql);
  //对占位符进行赋值
  $stmt->bind_param(
    "sssssisisss",
    $student["stuno"],
    $student["stuname"],
    $student["sex"],
    $student["phone"],
    $student["dorms"],
    $student["classid"],
    $student["major"],
    $student["departmentid"],
    $student["hobby"],
    $student["birth"],
    $student["photo"]
  );
  $result = $stmt->execute();
  if ($result) {
    $ajaxReturn = ["code" => 200, "msg" => "保存成功"];
  } else {
    $ajaxReturn = ["code" => 0, "msg" => "保存失败"];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//修改学生
function updateStudent($conn, $student)
{
  global $ajaxReturn;
  $base = "update xk_student  ";
  $condition = "";
  //学生姓名
  if (isset($student["stuname"]) && $student["stuname"] != "") {
    $condition .= " stuname='" . $student["stuname"] . "',";
  }
  //学生性别
  if (isset($student["sex"]) && $student["sex"] != "") {
    $condition .= " sex='" . $student["stuname"] . "',";
  }
  //电话
  if (isset($student["phone"]) && $student["phone"] != "") {
    $condition .= " phone='" . $student["phone"] . "',";
  }
  //宿舍号
  if (isset($student["dorms"]) && $student["dorms"] != "") {
    $condition .= " dorms='" . $student["dorms"] . "',";
  }
  //班级号
  if (isset($student["classid"]) && $student["classid"] != 0) {
    $condition .= " classid=" . $student["classid"] . ",";
  }
  //专业
  if (isset($student["major"]) && $student["major"] !== '') {
    $condition .= " major='" . $student["major"] . "',";
  }
  //部门
  if (isset($student["departmentid"]) && $student["departmentid"] != 0) {
    $condition .= " departmentid=" . $student["departmentid"] . ",";
  }
  //爱好
  if (isset($student["hobby"]) && $student["hobby"] !== '') {
    $condition .= " hobby='" . $student["hobby"] . "',";
  }
  //生日
  if (isset($student["birth"]) && $student["birth"] !== '') {
    $condition .= " birth='" . $student["birth"] . "',";
  }
  //照片
  if (isset($student["photo"]) && $student["photo"] !== '') {
    $condition .= " photo='" . $student["photo"] . "',";
  }
  if ($condition !== '') {
    //删除多余的逗号，
    $condition = substr($condition, 0, strlen($condition) - 1);
    $condition .= " where stuno='" . $student['stuno'] . "'";
    $base = $base . " set " . $condition;
    $result = mysqli_query($conn, $base);
    if ($result) {
      $ajaxReturn = ["code" => 200, "msg" => "修改成功"];
    } else {
      $ajaxReturn = ["code" => 0, "msg" => "修改失败"];
    }
  } else {
    $ajaxReturn = ["code" => 200, "msg" => "无修改项，无法修改"];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
//删除学生
function deleteStudent($conn, $stuno)
{
  global $ajaxReturn;
  $sql = "delete from xk_student where stuno='" . $stuno . "'";
  $result = mysqli_query($conn, $sql);
  if ($result) {
    $ajaxReturn = ["code" => 200, "msg" => "删除成功"];
  } else {
    $ajaxReturn = ["code" => 0, "msg" => "删除失败"];
  }
  return json_encode($ajaxReturn, JSON_UNESCAPED_UNICODE);
}
